package org.itheima.game.org.tank.game.model

import org.itheima.game.org.tank.game.Config
import org.itheima.game.org.tank.game.business.Attackable
import org.itheima.game.org.tank.game.business.Blockable
import org.itheima.game.org.tank.game.business.Movable
import org.itheima.game.org.tank.game.business.Sufferable
import org.itheima.game.org.tank.game.enum.Direction
import org.itheima.kotlin.game.core.Painter

/**
 * description ： 我方坦克
 * author : chenmengjia
 * email : cmjsp1990@163.com
 * date : 2020/3/14 15:00
 */
class Tank(
    override var x: Int,
    override var y: Int,
    override var width: Int = Config.block,
    override var height: Int = Config.block
) :
    Movable, Blockable, Sufferable {
    override var blood: Int = 20

    override fun notifySuffer(attackable: Attackable): Array<View>? {
        blood -= attackable.attackPower
        return arrayOf(Blast(x, y))
    }

    override var speed: Int = 8


    override var currentDirection = Direction.UP
    private var badDirection: Direction? = null

    override fun draw() {
        var imagePath = when (currentDirection) {
            Direction.UP -> "img/tank_u.gif"
            Direction.DOWN -> "img/tank_d.gif"
            Direction.LEFT -> "img/tank_l.gif"
            Direction.RIGHT -> "img/tank_r.gif"
        }
        Painter.drawImage(imagePath, x, y)

    }


    fun move(direction: Direction) {
        //判断碰撞
        if (direction == badDirection) {
            return
        }
        if (currentDirection != direction) {
            this.currentDirection = direction
            return
        }
        // this.currentDirection = direction
        when (currentDirection) {
            Direction.UP -> y -= speed
            Direction.DOWN -> y += speed
            Direction.LEFT -> x -= speed
            Direction.RIGHT -> x += speed

        }
        //
        if (x <= 0) x = 0
        if (x > Config.gameWidth - width) x = Config.gameWidth - width
        if (y < 0) y = 0
        if (x > Config.gameHeight - height) y = Config.gameHeight - height

    }


    override fun notifyCollision(direction: Direction?, blockable: Blockable?) {
        //接受碰撞信息
        this.badDirection = direction

    }

    fun shoot(): Bullet {
        //计算子弹坐标
        //如果坦克向上
        return Bullet(this,currentDirection) { bulletWidth, bulletHeight ->
            var tankY = y
            var tankX = x
            var tankWidth = width
            var tankHeight = height
            var bulletX = 0
            var bulletY = 0

            when (currentDirection) {
                Direction.UP -> {
                    bulletX = tankX + (tankWidth - bulletWidth) / 2
                    bulletY = tankY - bulletHeight / 2

                }
                Direction.DOWN -> {
                    bulletX = tankX + (tankWidth - bulletWidth) / 2
                    bulletY = tankY + tankHeight - bulletHeight / 2

                }
                Direction.LEFT -> {
                    bulletX = tankX + (tankWidth - bulletWidth) / 2
                    bulletY = tankY + (tankHeight - bulletHeight) / 2

                }
                Direction.RIGHT -> {
                    bulletX = tankX + tankWidth - bulletWidth / 2
                    bulletY = tankY + (tankHeight - bulletHeight) / 2

                }

            }
            Pair(bulletX, bulletY)
        }
    }
}